import { useState } from 'react'
import { getProductFabricPageList } from '@/api/goods'
export interface IUserPremissSupplier {
  fabricId: number
  fabricName: string
}
const useSearchFabric = (Key: 'fabricId' | 'fabricCode' | 'fabricName' = 'fabricId') => {
  let FabricTimeoutA: ITimeoutType
  const [FabricOption, setFabricOption] = useState<IOptionItem[]>([])
  const handleSearchFabric = (
    newValue = '',
    devSeason?: string,
    bigClassId?: number[] | string,
    intensiveStatus?: 0 | 1,
  ) => {
    if (FabricTimeoutA) {
      clearTimeout(FabricTimeoutA)
      FabricTimeoutA = null
    }
    const getOptions = async () => {
      const res = await getProductFabricPageList<{
        fabricName: string
        fabricCode: string
        fabricId: string
        devSeason: string
        supplierNames: string
        productFabricTypeVO: Record<string, any>
        supplierIds: any[]
      }>({
        searchName: newValue,
        status: 1,
        devSeason,
        bigClassId,
        intensiveStatus,
      })
      if (res.code === 200) {
        const FabricOption =
          res?.data?.list?.map((item) => {
            return {
              label: item.fabricName,
              value: item[Key],
              id: item.fabricId,
              code: item.fabricCode,
              supplierNames: item.supplierNames || null,
              fabricComposition: item.productFabricTypeVO?.fabricComposition || null,
              fabricBreadth: item.productFabricTypeVO?.fabricBreadth || null,
              yarnSpec: item.productFabricTypeVO?.yarnSpec || null,
              gramWeight: item.productFabricTypeVO?.gramWeight || null,
              devSeason: item?.devSeason || null,
              supplierIds: item?.supplierIds || null,
            }
          }) || []
        setFabricOption(FabricOption)
      }
    }
    FabricTimeoutA = setTimeout(getOptions, 500)
  }
  return { FabricOption, setFabricOption, handleSearchFabric }
}

export default useSearchFabric
